package com.yonyou.marketing.app.api.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.marketing.common.Response;
import org.marketing.common.exception.ServiceBizException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yonyou.marketing.app.api.entity.GscReservation;

@Service("appGetCarService")
public class AppGetCarService {
	
	private Logger          logger = Logger.getLogger(AppGetCarService.class);
	
	@Autowired
    private GetCarServiceInterface    getCarServiceInterface;
	
	public Map<String, Object> isCanGetSendCar(String dealerCode){
		logger.info("调用取送车服务  SERVICE-GETCAR==>start...");
		Response<Map<String, Object>> rList = getCarServiceInterface.isCanGetSendCar(dealerCode);
		Map<String, Object> result = new HashMap<>();
		if(rList!=null && rList.getResultCode()==1){
			result = rList.getData();
		}else if(rList!=null && rList.getResultCode()==0){
			throw new ServiceBizException(rList.getErrMsg());
		}else{
			throw new ServiceBizException("取送车服务异常");
		}
		return result;
	}
	public Map<String, Object> saveGetOrSendCar(String pkId){
		logger.info("调用取送车服务  SERVICE-GETCAR==>start...");
		Response<Map<String, Object>> rList = getCarServiceInterface.saveGscReservation(pkId);
		Map<String, Object> result = new HashMap<>();
		if(rList!=null && rList.getResultCode()==1){
			result = rList.getData();
		}else if(rList!=null && rList.getResultCode()==0){
			throw new ServiceBizException(rList.getErrMsg());
		}else{
			throw new ServiceBizException("取送车服务异常");
		}
		return result;
	}
	public Map<String, Object> saveWxOrder(String pkId){
		logger.info("调用取送车服务  SERVICE-GETCAR==>start...");
		Response<Map<String, Object>> rList = getCarServiceInterface.saveWxOrder(pkId);
		Map<String, Object> result = new HashMap<>();
		if(rList!=null && rList.getResultCode()==1){
			result = rList.getData();
		}else if(rList!=null && rList.getResultCode()==0){
			throw new ServiceBizException(rList.getErrMsg());
		}else{
			throw new ServiceBizException("取送车服务异常");
		}
		return result;
	}
	public Map<String, Object> updateWxState(String pkId,String sign){
		logger.info("调用取送车服务  SERVICE-GETCAR==>start...");
		Response<Map<String, Object>> rList = getCarServiceInterface.updateWxState(pkId,sign);
		Map<String, Object> result = new HashMap<>();
		if(rList!=null && rList.getResultCode()==1){
			result = rList.getData();
		}else if(rList!=null && rList.getResultCode()==0){
			throw new ServiceBizException(rList.getErrMsg());
		}else{
			throw new ServiceBizException("取送车服务异常");
		}
		return result;
	}
	public Map<String, Object> cancelGetSendCar(String etcoId){
		logger.info("调用取送车服务  SERVICE-GETCAR==>start...");
		Response<Map<String, Object>> rList = getCarServiceInterface.cancelCar(etcoId);
		Map<String, Object> result = new HashMap<>();
		if(rList!=null && rList.getResultCode()==1){
			result = rList.getData();
		}else if(rList!=null && rList.getResultCode()==0){
			throw new ServiceBizException(rList.getErrMsg());
		}else{
			throw new ServiceBizException("取送车服务异常");
		}
		return result;
	}
	public Map<String, Object> searchAddr(Integer searchType,String userId){
		logger.info("调用取送车服务  SERVICE-GETCAR==>start...");
		Response<Map<String, Object>> rList = getCarServiceInterface.searchAddr(searchType,userId);
		Map<String, Object> result = new HashMap<>();
		if(rList!=null && rList.getResultCode()==1){
			result = rList.getData();
		}else if(rList!=null && rList.getResultCode()==0){
			throw new ServiceBizException(rList.getErrMsg());
		}else{
			throw new ServiceBizException("取送车服务异常");
		}
		return result;
	}
	public List<Map<String, Object>> searchHistory(String addrName,String userId){
		logger.info("调用取送车服务  SERVICE-GETCAR==>start...");
		Response<List<Map<String, Object>>> rList = getCarServiceInterface.searchHistory(addrName,userId);
		List<Map<String, Object>> result = new ArrayList<>();
		if(rList!=null && rList.getResultCode()==1){
			result = rList.getData();
		}else if(rList!=null && rList.getResultCode()==0){
			throw new ServiceBizException(rList.getErrMsg());
		}else{
			throw new ServiceBizException("取送车服务异常");
		}
		return result;
	}
	public Map<String, Object> getCarInfo(String userId){
		logger.info("调用取送车服务  SERVICE-GETCAR==>start...");
		Response<Map<String, Object>> rList = getCarServiceInterface.getCarInfo(userId);
		Map<String, Object> result = new HashMap<>();
		if(rList!=null && rList.getResultCode()==1){
			result = rList.getData();
		}else if(rList!=null && rList.getResultCode()==0){
			throw new ServiceBizException(rList.getErrMsg());
		}else{
			throw new ServiceBizException("取送车服务异常");
		}
		return result;
	}
	@SuppressWarnings("unchecked")
	public Map<String, Object> validateGscDistance(GscReservation gscRes,String userId){
		logger.info("调用取送车服务  SERVICE-GETCAR==>start...");
		Response<Map<String, Object>> rList = getCarServiceInterface.validateGscDistance(gscRes,userId);
		Map<String, Object> result = new HashMap<>();
		if(rList!=null && rList.getResultCode()==1){
			Map<String, Object> result1 = rList.getData();
			if("1".equals(result1.get("resultCode").toString())){
				result = (Map<String, Object>) result1.get("data");
			}else{
				throw new ServiceBizException(result1.get("errMsg")==null?"系统异常请稍后再试！":result1.get("errMsg").toString());
			}
		}else if(rList!=null && rList.getResultCode()==0){
			throw new ServiceBizException(rList.getErrMsg());
		}else{
			throw new ServiceBizException("取送车服务异常");
		}
		return result;
	}
	public List<Map<String, Object>> queryCar(String userId){
		logger.info("调用取送车服务  SERVICE-GETCAR==>start...");
		Response<List<Map<String, Object>>> rList = getCarServiceInterface.queryCar(userId);
		List<Map<String, Object>> result = new ArrayList<>();
		if(rList!=null && rList.getResultCode()==1){
			result = rList.getData();
		}else if(rList!=null && rList.getResultCode()==0){
			throw new ServiceBizException(rList.getErrMsg());
		}else{
			throw new ServiceBizException("取送车服务异常");
		}
		return result;
	}
	public Map<String, Object> queryQscDetail(String etcoId){
		logger.info("调用取送车服务  SERVICE-GETCAR==>start...");
		Response<Map<String, Object>> rList = getCarServiceInterface.queryQscDetail(etcoId);
		Map<String, Object> result = new HashMap<>();
		if(rList!=null && rList.getResultCode()==1){
			result = rList.getData();
		}else if(rList!=null && rList.getResultCode()==0){
			throw new ServiceBizException(rList.getErrMsg());
		}else{
			throw new ServiceBizException("取送车服务异常");
		}
		return result;
	}
	public Map<String, Object> queryQscProgress(String etcoId){
		logger.info("调用取送车服务  SERVICE-GETCAR==>start...");
		Response<Map<String, Object>> rList = getCarServiceInterface.queryQscProgress(etcoId);
		Map<String, Object> result = new HashMap<>();
		if(rList!=null && rList.getResultCode()==1){
			result = rList.getData();
		}else if(rList!=null && rList.getResultCode()==0){
			throw new ServiceBizException(rList.getErrMsg());
		}else{
			throw new ServiceBizException("取送车服务异常");
		}
		return result;
	}
	public Map<String, Object> queryQscTrack(String etcoId){
		logger.info("调用取送车服务  SERVICE-GETCAR==>start...");
		Response<Map<String, Object>> rList = getCarServiceInterface.queryQscTrack(etcoId);
		Map<String, Object> result = new HashMap<>();
		if(rList!=null && rList.getResultCode()==1){
			result = rList.getData();
		}else if(rList!=null && rList.getResultCode()==0){
			throw new ServiceBizException(rList.getErrMsg());
		}else{
			throw new ServiceBizException("取送车服务异常");
		}
		return result;
	}
	public Map<String, Object> queryDriverinfo(String etcoId){
		logger.info("调用取送车服务  SERVICE-GETCAR==>start...");
		Response<Map<String, Object>> rList = getCarServiceInterface.queryDriverinfo(etcoId);
		Map<String, Object> result = new HashMap<>();
		if(rList!=null && rList.getResultCode()==1){
			result = rList.getData();
		}else if(rList!=null && rList.getResultCode()==0){
			throw new ServiceBizException(rList.getErrMsg());
		}else{
			throw new ServiceBizException("取送车服务异常");
		}
		return result;
	}
}
